package com.example.demo.mapimage;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;

public class Search {

	
	public static void main(String[] args) {
		//获取成都市所有的幼儿园地理位置高德地图坐标
		for(int i=0;i<2000;i++) {
			//发送http请求
			String result = sendGet(i);
			List<LocalInfo> info = resoveInfo(i, result);
			if(CollectionUtil.isEmpty(info)) {
				break;
			}
		}
		
	}
	public static String sendGet(int curry_page) {
		String url = "https://restapi.amap.com/v3/place/text?keywords=幼儿园&city=成都&output=json&offset=20&page="+curry_page+"&key=ce5d137ca621f6e682131478c448eb4a&extensions=all";
		String result = HttpUtil.get(url);
		return result;
	}
	
	
	public static List<LocalInfo>  resoveInfo(int curry_page,String result) {
		List<LocalInfo> infos = new ArrayList<>();
		if(StrUtil.isNotBlank(result)) {
			JSONObject jsonObject = JSONObject.parseObject(result);
			JSONArray jsonArray = jsonObject.getJSONArray("pois");
			for (Iterator iterator = jsonArray.iterator(); iterator.hasNext();) {
			      JSONObject job = (JSONObject) iterator.next();
			      LocalInfo localInfo = new LocalInfo();
			      localInfo.setName(job.get("name").toString());
			      localInfo.setAddress(job.get("address").toString());
			      System.out.println();
			      String x = (job.get("location").toString()).split(",")[0];
			      String y = (job.get("location").toString()).split(",")[1];
			      localInfo.setX(x);
			      localInfo.setY(y);
			      localInfo.setTelphone("13896667100");
			      //添加到云图
			      //拼接json
			      String json = "{_name:\""+localInfo.getName()+"\",_location:\""+job.get("location").toString()+"\",_address:\""+job.get("address").toString()+"\"}";
			      String url = "https://yuntuapi.amap.com/datamanage/data/create?key=ce5d137ca621f6e682131478c448eb4a&tableid=5bf5041b2376c1673496ca92&data="+json;
			      String result1 = HttpUtil.get(url);
			      System.out.println(result1);
			      infos.add(localInfo);
			    }
		}
		return infos;
	}
	
	//高德地图坐标转百度地图坐标
	public static String changeLon(String localtion) {
		String url = "https://restapi.amap.com/v3/assistant/coordinate/convert?locations="+localtion+"&coordsys=baidu&output=json&key=ce5d137ca621f6e682131478c448eb4a";
		String result = HttpUtil.get(url);
		JSONObject jsonObject = JSONObject.parseObject(result);
		return jsonObject.getString("locations");
	}
	
}
